<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

$GLOBALS['_XPARAMS'] = array();

function otx_param_set($key, $value)
{
	$GLOBALS['_XPARAMS'][$key] = $value;
}

function otx_param_get($key, $default = null)
{
	if (isset($GLOBALS['_XPARAMS'][$key])) {
		return $GLOBALS['_XPARAMS'][$key];
	} else if (isset($_POST[$key])) {
		return $_POST[$key];
	} else if (isset($_GET[$key])) {
		return $_GET[$key];
	} else {
		return $default;
	}
}

function otx_param_unset($key)
{
	if (isset($GLOBALS['_XPARAMS'][$key])) {
		unset($GLOBALS['_XPARAMS'][$key]);
	}
	if (isset($_POST[$key])) {
		unset($_POST[$key]);
	}
	if (isset($_GET[$key])) {
		unset($_GET[$key]);
	}
}

function otx_param_exists($key)
{
	if (isset($GLOBALS['_XPARAMS'][$key])) {
		return true;
	} else if (isset($_POST[$key])) {
		return true;
	} else if (isset($_GET[$key])) {
		return true;
	} else {
		return false;
	}
}

function otx_param_string($key, $default = '')
{
	return trim(strval(otx_param_get($key, $default)));
}

function otx_param_int($key, $default = 0)
{
	return intval(otx_param_get($key, $default));
}

function otx_param_bool($key, $default = false)
{
	return otx_param_int($key) ? true : $default;
}

function otx_param_array($key, $default = array())
{
	$value = otx_param_get($key);
	if (is_array($value)) {
		return $value;
	} else if (strlen($value) > 0) {
		return explode(',', $value);
	} else {
		return $default;
	}
}
